﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace Core.Unity {
    public class CommandAdapter : ICommand, INotifyPropertyChanged {
        public event EventHandler CanExecuteChanged;
        public event PropertyChangedEventHandler PropertyChanged;
        private CommandBase _command;

        public bool IsEnabled {
            get {
                return _command.IsEnabled;
            }
            set {
                _command.IsEnabled = value;
                if (this.PropertyChanged != null) {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("IsEnabled"));
                }
            }
        }

        public CommandBase Command {
            get {
                return _command;
            }
            set {
                _command = value;
            }
        }

        public CommandAdapter(CommandBase command) {
            _command = command;
            _command.PropertyChanged += _command_PropertyChanged;
        }

        void _command_PropertyChanged(object sender, PropertyChangedEventArgs e) {
            if (this.PropertyChanged != null) {
                this.PropertyChanged(this, e);
            }
        }

        public bool CanExecute(object parameter) {
            return true;
        }

        public void Execute(object parameter) {
            _command.OnClick(parameter);
        }

        public void OnCreate(object hook) {
            _command.OnCreate(hook);
        }
    }
}
